<template>
  <div class="boxs">
    <el-card>
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item :label="$t('review.templateContent')" prop="name">
          <el-input
            v-model="queryParams.template_content"
            :placeholder="$t('review.templateContent')"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item :label="$t('review.status')">
          <el-select v-model="queryParams.status" @change="handleQuery">
            <el-option
              key="0"
              :label="$t('review.Normal')"
              value="0"
            ></el-option>
            <el-option
              key="2"
              :label="$t('review.Disable')"
              value="2"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
            >{{$t('table.search')}}</el-button
          >
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
            >{{$t('table.reset')}}</el-button
          >
        </el-form-item>
      </el-form>
      <div class="flex_between">
        <span>{{$t('review.templateContent')}}</span>
        <el-button
          icon="el-icon-plus"
          type="primary"
          plain
          size="small"
          @click="openDialog"
          >{{$t('review.addTemplate')}}</el-button
        >
      </div>

      <el-table
        border
        :data="moreArr"
        style="margin-top: 15px"
        v-loading="loading"
      >
        <el-table-column label="index" align="center" width="60" type="index" />
        <el-table-column
          :label="$t('review.language')"
          align="center"
          width="100"
          prop="reviewer_language"
        />

        <el-table-column
          show-overflow-tooltip
          :label="$t('review.templateContent')"
          align="center"
          prop="template_content"
        />
        <el-table-column :label="$t('review.status')" width="100" align="center" prop="status">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.status == '2'">{{$t('review.Disable')}}</el-tag>

            <el-tag type="success" v-if="scope.row.status == '0'">{{$t('review.Normal')}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="300" :label="$t('review.operate')" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >{{$t('review.Edit')}}</el-button
            >
            <el-button
              size="mini"
              type="success"
              v-if="scope.row.status == '2'"
              @click="editStatusHandel(scope.$index, scope.row)"
              >{{$t('review.Enables')}}</el-button
            >
            <el-button
              size="mini"
              type="warning"
              v-if="scope.row.status == '0'"
              @click="editStatusHandel(scope.$index, scope.row)"
              >{{$t('review.Disable')}}</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >{{$t('review.Delete')}}</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.size"
        @pagination="getMore"
      />
    </el-card>

    <el-dialog :title="$t('review.operate')" :visible.sync="dialogVisible" width="630px">
      <el-form
        class="demo-form-inline"
        label-width="140px"
        :model="form"
        :rules="rules"
        ref="ruleForm"
      >
        <el-form-item :label="$t('review.language')" prop="reviewer_language">
          <el-select v-model="form.reviewer_language" style="width: 100%">
            <el-option
              v-for="item in languageList"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('review.templateContent')" prop="template_content">
          <el-input
            type="textarea"
            v-model="form.template_content"
            :placeholder="$t('review.templateContent')"
            :autosize="{ minRows: 4 }"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">{{$t('review.close')}}</el-button>
        <el-button :type="form.id ? 'warning' : 'primary'" @click="affirm">{{
          form.id ? $t('review.confirm') : $t('review.confirm')
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
      <script>
import {
  selectTemplateFive,
  saveTemplateFive,
  getLanguage,
  deleteTemplate,
  setTemplateStatus,
} from "@/api/appComment";
export default {
  name: "fiveTemplate",
  data() {
    return {
      queryParams: {
        page: 1,
        size: 10,
        template_content: "",
        status: "",
      },
      languageList: [],
      package_name: "",
      total: 0,
      loading: false,
      addLoading: false,
      moreArr: [],
      dialogVisible: false,
      form: {
        name: "",
        remark: "",
      },
      search: "",
      rules: {
        template_content: [
          { required: true, message: "Please enter the template content", trigger: "blur" },
        ],
        reviewer_language: [
          { required: true, message: "Please select language", change: "blur" },
        ],
        type_id: [{ required: true, message: "Please choose the type", change: "blur" }],
      },
    };
  },
  created() {
    const { package_name } = this.$route.query;
    this.package_name = package_name;
    this.setQueryParams(package_name);
    this.getLanguageList();
  },
  methods: {
    async getMore() {
      this.loading = true;
      let { data, total } = await selectTemplateFive(this.queryParams);
      this.loading = false;
      this.moreArr = data;
      this.total = total;
    },
    async getLanguageList() {
      let { data } = await getLanguage({
        package_name: this.package_name,
      });
      this.languageList = data.map((item) => ({
        id: item.reviewer_language,
        name: item.reviewer_language,
      }));
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getMore();
    },
    resetQuery() {
      this.queryParams.status = null;
      this.queryParams.template_content = null;
      this.queryParams.page = 1;
      this.getMore();
    },
    setQueryParams(package_name) {
      this.queryParams.package_name = package_name;
      this.getMore();
    },
    async affirm() {
      this.form.package_name = this.package_name;
      this.$refs["ruleForm"].validate(async (valid) => {
        if (!valid) return;
        try {
          this.addLoading = true;
          await saveTemplateFive(this.form);
          this.addLoading = false;
          this.$message.success(this.form.id ? "success!" : "success!");
          this.getMore();
          this.dialogVisible = false;
        } catch (e) {
          this.addLoading = false;
        }
      });
    },
    openDialog() {
      this.form = {};
      this.dialogVisible = true;
    },
    handleEdit(i, row) {
      this.form = { ...row };
      this.dialogVisible = true;
    },
    editStatusHandel(i, row) {
      let content = "";
      let type = ''
      if (row.status == 0) {
        content = this.$t('review.isDisable');
        type='error'
      } else {
        type='success'
        content = this.$t('review.isEnables');
      }
      this.$confirm(content,this.$t('review.deleteText2'), {
        confirmButtonText: this.$t('review.confirm'),
        cancelButtonText: this.$t('review.close'),
        type: type,
      }).then(async () => {
        await setTemplateStatus({ id: row.id,status:row.status });
        this.getMore();
        this.$message({
          message: "success!",
          type: "success",
        });
      });
    },
    handleDelete(index, row) {
      this.$confirm(this.$t('review.deleteText1'), this.$t('review.deleteText2'), {
        confirmButtonText: this.$t('review.confirm'),
        cancelButtonText: this.$t('review.close'),
        type: "error",
      }).then(async () => {
        await deleteTemplate({ id: row.id });
        this.getMore();
        this.$message({
          message: "success!",
          type: "success",
        });
      });
    },
  },
};
</script>
      <style lang="scss" scoped>
.boxs {
  padding: 20px;
}
.el-form-item {
  margin-right: 20px;
}
.el-textarea__inner {
  min-height: 200px !important;
}
</style>
      